(function () {
  var stateMap, elemMap, setElemMap, bindElemMap, handleClickSignNow;

  stateMap = {
    lon: 0,
    lat: 0
  };

  handleClickSignNow = function () {
    if (Number(elemMap.phoneIpt.value).toString().length !== 11) {
      util.toast('请输入正确的手机号！');
      return false;
    }

    if (Number(elemMap.codeIpt.value).toString().length !== 6) {
      util.toast('请输入正确的验证码！');
      return false;
    }

    if (!util.validatePwd(elemMap.pwdIpt.value)) {
      util.toast('请输入正确格式的密码！');
      return false;
    }

    if (elemMap.pwdIpt.value !== elemMap.pwdAgainIpt.value) {
      util.toast('两次密码不一致！');
      return false;
    }

    //禁止多次点击提交
    elemMap.signNow.style.pointerEvents = 'none';
    util.addFetchLoading();

    $.ajax({
      url: util.hostUrl + '/v1/userInfo/login.html',
      type: 'post',
      data: {
        header: JSON.stringify({
          reqKey: util.reqKey
        }),
        param: JSON.stringify({
          loginType: 3,
          phone: elemMap.phoneIpt.value,
          code: elemMap.codeIpt.value,
          pass: elemMap.pwdIpt.value,
          platform: 3,
          channel: localStorage.jqyChannel,
          longitude: stateMap.lon,
          latitude: stateMap.lat,
          app: util.app
        })
      },
      success: function (res) {
        /*************S 记录点击注册************/
        var t = JSON.parse(localStorage.TJBasic);
        t.CurrentPage = '注册';
        t.time = (new Date()).valueOf();
        t.behavior = '点击立即注册';
        t.sfLogin = localStorage.jqyPhone ? '是' : '否';
        t.loginType = '密码登录';
        t.state = res.result.msg;

        var arr = JSON.parse(localStorage.TJArray);
        arr.push(t);
        localStorage.TJArray = JSON.stringify(arr);
        /*************E 记录点击注册************/

        if (res.result.code == 1 || res.result.code == 6) {
          localStorage.jqyToken  = res.result.token;
          localStorage.jqyCookie = res.result.cookie;
          localStorage.jqyUserId = res.response.userId;
          localStorage.jqyPhone  = res.response.phone;

          //完善统计信息
          var temp = JSON.parse(localStorage.TJBasic);
          temp.phone = res.response.phone;
          temp.userId = res.response.userId;
          localStorage.TJBasic = JSON.stringify(temp);

          location.replace('./index.html');
        } else if (res.result.code == 3) {
          util.toast('请获取验证码！');
        } else if (res.result.code == 4) {
          util.toast('验证码失效！');
        } else if (res.result.code == 8) {
          util.toast('验证码不匹配！');
        } else if (res.result.code == 11) {
          util.toast('该用户已注册！');
        } else {
          util.toast('Oops,发生错误！');
        }
      },
      error: function () {
        util.toast('Oops,网络有问题！');
      },
      complete: function () {
        elemMap.signNow.style.pointerEvents = 'auto';
        util.removeFetchLoading();
      }
    });
  };

  bindElemMap = function () {
    elemMap.phoneIpt.addEventListener('keyup', function () {
      util.showOrHideClearBtn(this, this.parentNode.querySelector('.ipt-clear'));
    });
    elemMap.pwdIpt.addEventListener('keyup', function () {
      util.showOrHidePreviewBtn(this, this.parentNode.querySelector('.ipt-eye'));
    });
    elemMap.pwdAgainIpt.addEventListener('keyup', function () {
      util.showOrHidePreviewBtn(this, this.parentNode.querySelector('.ipt-eye'));
    });
    elemMap.codeIpt.addEventListener('keyup', function () {
      util.showOrHideClearBtn(this, this.parentNode.querySelector('.ipt-clear'));
    });

    util.nList2Arr(document.querySelectorAll('.ipt-clear')).forEach(function (btn) {
      btn.addEventListener('click', function () {
        util.clearIpt(this.parentNode.querySelector('input'), this);
      });
    });

    util.nList2Arr(document.querySelectorAll('.ipt-eye')).forEach(function (btn) {
      btn.addEventListener('click', function () {
        util.changePwdState(this.parentNode.querySelector('input'), this);
      });
    });

    elemMap.getCode.addEventListener('click', function () {
      if (Number(elemMap.phoneIpt.value).toString().length === 11) {
        util.fetchCode(this, elemMap.phoneIpt.value, 3);
      } else {
        util.toast('请输入手机号！');
      }
    });

    elemMap.signNow.addEventListener('click', handleClickSignNow);
  };

  setElemMap = function () {
    elemMap = {
      phoneIpt    : document.querySelector('#phoneIpt'),
      pwdIpt      : document.querySelector('#pwdIpt'),
      pwdAgainIpt : document.querySelector('#pwdAgainIpt'),
      codeIpt     : document.querySelector('#codeIpt'),
      getCode     : document.querySelector('.get-code'),
      signNow     : document.querySelector('.sign-now')
    };
  };

  setElemMap();
  bindElemMap();
  util.setPageHeight();
  util.getLocation(function (position) {
    stateMap.lat = position.coords.latitude;
    stateMap.lon = position.coords.longitude;
  });
}());

